package com.tianbing.adlottery.controller.user;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.fengniu.ejb.business.IUserBusiness;
import com.fengniu.ejb.dao.IContentDao;
import com.fengniu.ejb.dao.ISiteDao;
import com.fengniu.ejb.dao.IUserDao;
import com.fengniu.ejb.entity.Content;
import com.fengniu.ejb.entity.Site;
import com.fengniu.ejb.entity.Site2Content;
import com.fengniu.ejb.entity.User2Content;
import com.fengniu.ejb.entity.User2Site;
import com.tianbing.adlottery.global.IConstants;
import com.tianbing.adlottery.helper.SkinUtils;
import com.tianbing.adlottery.util.ControllerFormBean;
import com.tianbing.adlottery.util.UserSession;
import com.tianbing.adlottery.util.UtilDataGroup;

public class EditMyFloatBookController extends SimpleFormController {
	static Logger logger = Logger.getLogger(EditMyFloatBookController.class.getName());

	private IUserDao userDao;

	private IContentDao contentDao;

	private ISiteDao siteDao;

	private IUserBusiness userBusiness;

	private SkinUtils skinUtils;

	// ------------------GET Request LifeCycle------------------
	protected Object formBackingObject(HttpServletRequest request) throws Exception {
		ControllerFormBean fb = new ControllerFormBean();
		long u2csid = ServletRequestUtils.getLongParameter(request, "u2csid", -1);
		User2Content u2c = userDao.findUser2ContentBy(u2csid);
		if (u2c == null) {
			u2c = new User2Content();
			long csid = ServletRequestUtils.getLongParameter(request, "csid", -1);
			Content content = contentDao.findContentBySysId(csid);
			u2c.setContent(content);
		}
		fb.setUser2Content(u2c);
		return fb;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse, java.lang.Object,
	 *      org.springframework.validation.BindException)
	 */
	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object cmd,
			BindException ex) throws Exception {

		ModelAndView mv = new ModelAndView(this.getSuccessView());
		ControllerFormBean form = (ControllerFormBean) cmd;
		User2Content u2c = form.getUser2Content();
		long[] floatInSiteSysIds = ServletRequestUtils.getLongParameters(request, "siteSysId");
		UserSession auth = skinUtils.getUserAuthorization(request, null);
		userBusiness.saveUserFloatBook(auth.getUserSysId(), u2c, floatInSiteSysIds);
		return mv;
	}

	@SuppressWarnings("unchecked")
	protected Map referenceData(HttpServletRequest request) throws Exception {
		Map model = new HashMap();

		UserSession auth = skinUtils.getUserAuthorization(request, null);
		// --------------------
		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = ServletRequestUtils.getIntParameter(request, "pgsize", 30);
		long u2csid = ServletRequestUtils.getLongParameter(request, "u2csid", -1);
		long csid = ServletRequestUtils.getLongParameter(request, "csid", -1);
		long ssid = ServletRequestUtils.getLongParameter(request, "ssid", -1);
		Integer[] relationIds = { IConstants.RelationID.S2C_IS_POSTEE_OF.getId() };
		Integer[] contentTypeIds = { IConstants.Type.CONTENT_MESSAGE.getId() };
		List<Site2Content> s2cs = siteDao.getListSite2ContentBy(-1, relationIds, csid, null, contentTypeIds, null,
				null, pgno, pgsize, null);
		// ---------------
		List<User2Site> user2StoreList = userBusiness.getListUserFavoriteHorde(auth.getUserSysId(), -1, 0, 20);
		HashMap<Long, String> postedFlags = new HashMap();
		if (user2StoreList != null && s2cs != null) {
			for (int i = 0; i < user2StoreList.size(); i++) {
				User2Site u2s = user2StoreList.get(i);
				for (int j = 0; j < s2cs.size(); j++) {
					Site2Content s2c = s2cs.get(j);
					if (s2c.getSite().getSysId().compareTo(u2s.getSite().getSysId()) == 0) {
						postedFlags.put(u2s.getSite().getSysId(), "Y");
					}
				}
			}

		}
		List user2StoreGroupList = UtilDataGroup.makeGroups(user2StoreList, 0, 4);

		// -------get site
		Site site = siteDao.findSiteBySysId(ssid);

		model.put("postDefaultSite", site);
		model.put("user2StoreGroupList", user2StoreGroupList);
		model.put("postedFlags", postedFlags);

		model.put("u2csid", u2csid);
		model.put("csid", csid);
		model.put("ssid", ssid);

		return model;
	}

	public void setSkinUtils(SkinUtils skinUtils) {
		this.skinUtils = skinUtils;
	}

	public static void setLogger(Logger logger) {
		EditMyFloatBookController.logger = logger;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	public void setUserBusiness(IUserBusiness userBusiness) {
		this.userBusiness = userBusiness;
	}

	public void setSiteDao(ISiteDao siteDao) {
		this.siteDao = siteDao;
	}

	public void setContentDao(IContentDao contentDao) {
		this.contentDao = contentDao;
	}

}
